package org.nowcoder.leetcode.hashTable;


import java.util.HashMap;
import java.util.Map;

/**
 * Title  : 290. Word Pattern
 * Source : https://leetcode.cn/problems/word-pattern/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-01
 */
public class LeetCode_290 {
    public boolean wordPattern(String pattern, String s) {
        String[] words = s.split(" ");
        Map<Character, String> map = new HashMap<>();
        //边界条件
        if (pattern.length() != words.length) {
            return false;
        }
        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (map.get(c) != null) {
                if (!map.get(c).equals(words[i])) {
                    return false;
                }
            } else {
                //不存在的情况
                if (!map.containsValue(words[i])) {
                    map.put(c, words[i]);
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}
